// pages/pay/pay.js
var app = getApp();
Page({
  data: {
    getcodes: "获取验证码",
    canGetCode: false
  },

  onLoad: function (options) {
    // 页面初始化 options为页面跳转所带来的参数
    if (options.direct) {
      this.setData({
        isDirect: options.direct,
      })
    }
    this.setData({
      addressDetaile: app.addressDetailes
    })
    var that = this;
    wx.getSystemInfo({//获取滑块的高宽
      success: function (res) {
        //console.log(res)
        that.setData({
          winWidth: res.windowWidth,
          winHeight: res.windowHeight
        });
      }
    });
  },
  onReady: function () {
    // 页面渲染完成
    // console.log(wx.getStorageSync('countdowns'))
    wx.setStorageSync('countdowns', 60)
  },
  onShow: function () {
    // 页面显示
    if (wx.getStorageSync('youu') == 11) {//如果倒计时的时间还没好，就让他继续倒计时，当cookie等于1时不能倒计时，为2可以
      wx.setStorageSync("cookie", 2)
      tttt(this)
    }
    // else if (wx.getStorageSync('youu') == 22) {
    //   wx.setStorageSync("cookie", 2)
    // }
    else {
      wx.setStorageSync("cookie", 2)
    }
  },
  onHide: function () {
    // 页面隐藏
  },
  onUnload: function () {
    // 页面关闭
    wx.setStorageSync('countdowns', this.data.countdowns);
    wx.setStorageSync('cookie', 1);
  },
  onShareAppMessage: function (res) {
    return {
      title: '山野土菜馆',
      path: '/pages/homepage/homepage?id=' + wx.getStorageSync("userid"),
    }
  },
  inputUser: function (e) {
    if (e.detail.value != '') {
      this.setData({
        inputUser: e.detail.value
      })
    }
  }, inputAddress: function (e) {
    if (e.detail.value != '') {
      this.setData({
        inputAddress: e.detail.value
      })
    }
  }, inputPhone: function (e) {
    if (e.detail.value != '') {
      this.setData({
        inputPhone: e.detail.value
      })
    }

  },
  inputGetCode: function (e) {
    var that = this;
    if (e.detail.value != "") {
      this.setData({
        inputGetCode: e.detail.value
      })
    }
  },
  showtime: function () {
    var that = this;
    var reg = /^1[34578]\d{9}$/
    if (reg.test(that.data.inputPhone)) {
      tttt(this)
    } else {
      wx.showToast({
        title: "请输入正确的手机号",
        icon: 'loading',
        duration: 2000
      })
    }
  },
  returnPay: function () {
    //console.log(111)
    var that = this;
    var superID;
    var reg = /^1[34578]\d{9}$/
    if (reg.test(that.data.inputPhone)) {
    } else {
      wx.showToast({
        title: "请输入正确的手机号",
        icon: 'loading',
        duration: 3000
      })
      return;
    }
    if (that.data.inputUser && that.data.inputPhone && that.data.inputAddress){

    }else{
      wx.showToast({
        title: '请填写完整信息',
        icon: "loading"
      })
     return;
   
    }
   /* if (that.data.isDirect == 1) {//判断是直营新建地址还是超市新建地址
      superID = -1
    } else {
      superID = wx.getStorageSync('superID')
    }*/
    wx.request({
      url: app.globalData.allUrl + "/sMSVerificationAction/verificationCode",
      data: {
        userId: wx.getStorageSync('userid'),
        code: that.data.inputGetCode,
        phone: that.data.inputPhone,
        addjson: {
          userId: wx.getStorageSync('userid'),
          phone: that.data.inputPhone,
          consignee: that.data.inputUser,
          expressageAdd: that.data.inputAddress,
          locationAdd: app.addressDetailes,
          longitudeNum: wx.getStorageSync('longitude'),
          latitudeNum: wx.getStorageSync('latitude'),
          superId: -1,
        }
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
        //console.log(res.data)
        if (res.data) {
          wx.setStorageSync('addressID', res.data.id)
          wx.setStorageSync('addInputUser', res.data.consignee);
          wx.setStorageSync('addInputAddress', res.data.expressageAdd);
          wx.setStorageSync('addInputPhone', res.data.phone);
          wx.setStorageSync('addSuperID', res.data.superId);
          wx.setStorageSync('addLocationAdd', res.data.locationAdd)
          wx.showToast({
            title: '提交订单成功',
            icon: 'success',
            duration: 2000
          });
          // app.inputUser = that.data.inputUser;
          // app.inputAddress = that.data.inputAddress;
          // app.inputPhone = that.data.inputPhone;
          // app.inputGetCode = that.data.inputGetCode;
          // wx.setStorageSync('inputUser', this.data.inputUser);
          // wx.setStorageSync('inputAddress', this.data.inputAddress);
          // wx.setStorageSync('inputPhone', this.data.inputPhone);
          if (that.data.isDirect == 1) {
            var addDirectAddress = {
              consignee: res.data.consignee,
              phone: res.data.phone,
              expressageadd: res.data.expressageAdd,
              locationadd: res.data.locationAdd,
              id: res.data.id,
            }
            //console.log(addDirectAddress)
            wx.setStorageSync('checkboxchooseaddress', addDirectAddress)
          }
          wx.navigateBack({
            delta: 1
          })
        } else {
          wx.showToast({
            title: "输入验证码错误",
            icon: 'loading',
            duration: 2000
          })
        }
      }
    })

  },

  getCode: function () {//从后台获取验证码
    if (wx.getStorageSync('countdowns') == 60) {
      getCodes(this);


    }
  }
  , getPhoneNumber: function (e) {
    var that=this;
    console.log(e.detail.errMsg)
    console.log(e.detail.iv)
    console.log(e.detail.encryptedData)
    wx.request({
      url: app.globalData.allUrl + "/utilAction/getUserPhone",
      data: {
        encryptedData: e.detail.encryptedData,
        iv: e.detail.iv,
        userID:wx.getStorageSync("userid"),
      },
      method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
      // header: {}, // 设置请求的 header
      success: function (res) {
    
        that.setData({
          userPhone: res.data.phoneNumber,
          inputPhone: res.data.phoneNumber
        })
      }

    })
  } 
})

function getCodes(that) {

  var countdown = wx.getStorageSync('countdowns');
  if (countdown == 0) {
    that.setData({
      getcodes: "免费获取验证码",
      canGetCode: false
    })
    wx.setStorageSync('countdowns', 60);
  } else {
    countdown--;
    // val.setAttribute("disabled", true);
    wx.setStorageSync('countdowns', countdown)
    that.setData({
      getcodes: "重新发送(" + wx.getStorageSync('countdowns') + ")",
      canGetCode: true,
      countdowns: countdown
    })
    setTimeout(function () {
      getCodes(that);
    }, 1000)
  }

}


function tttt(that) {//倒计时函数
  if (wx.getStorageSync('cookie') == 1) {
    return
  }
  if (!wx.getStorageSync('nowtimesss')) {
    wx.setStorageSync('nowtimesss', new Date().getTime())
    //在此处发起请求
    getAdCode(that)
  }
  var oldTime = wx.getStorageSync('nowtimesss');
  var newTime = new Date().getTime();
  if (parseInt((newTime - oldTime) / 1000) < 60) {
    wx.setStorageSync('youu', 11)
    //console.log(60-parseInt((newTime-oldTime)/1000))
    that.setData({
      canGetCodes: true,
      showtimess: "(" + (60 - parseInt((newTime - oldTime) / 1000)) + ")"
    })
  } else {
    wx.setStorageSync('youu', 22)
    wx.setStorageSync('nowtimesss', "")
    wx.setStorageSync('nowtimesss', "")
    that.setData({
      showtimess: "",
      canGetCodes: false,
    });
    return;
  }
  setTimeout(function () {
    tttt(that)
  }, 1000)
}

function getAdCode(that) {

  wx.request({
    url: app.globalData.allUrl + "/sMSVerificationAction/sendNote",
    data: {
      recNum: that.data.inputPhone,
      userId: wx.getStorageSync('userid'),
    },
    method: 'GET', // OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE, CONNECT
    // header: {}, // 设置请求的 header
    success: function (res) {
      // success
    }
  
  })


}